﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.Threading;

namespace CinemaBookingWcfService
{
    public class BookingSeats : IBookingSeats
    {
        dbCinemaBookingDataContext db = new dbCinemaBookingDataContext();
        ICinemaWcf wcf = new CinemaWcf();

        //Threading is considered, but not used
        public void CreateBookingThreads(int numberOfSeats, int seatChoiceID, int screeningID, string name, int phone)
        {
            new Thread(() =>
            {
                CreateBooking(numberOfSeats, seatChoiceID, screeningID, name, phone);
            }).Start();
        }

        public bool CreateBooking(int numberOfSeats, int seatChoiceID, int screeningID, string name, int phone)
        {
            List<int> SeatList = GetSeatsForBooking(numberOfSeats, seatChoiceID, screeningID);

            if (SeatList.Contains(0))
            {
                return false;
            }
            else
            {
                wcf.InsertOrder(name, phone);
                var orderID = (from o in db.CinemaOrders
                               where o.OrderName == name && o.OrderPhone == phone
                               select o.OrderID).Max();


                foreach (int id in SeatList)
                {
                    wcf.insertPartOrder(orderID, id, screeningID);
                    wcf.InsertBooked(id, screeningID, orderID);
                }
                return true;
            }
        }

        public List<int> GetSeatsForBooking(int numberOfSeats, int seatChoiceID, int screeningID)
        {
            int seatChoice;
            int rowChoice;
            List<int> seats = new List<int>();
            seatChoice = wcf.GetSeatNumberByID(seatChoiceID);
            rowChoice = wcf.GetRowIDBySeatID(seatChoiceID);
            int rowLength = wcf.GetRowSize(rowChoice);

            for (int i = numberOfSeats; i > 0; i--)
            {
                int newSeatNr;
                int newSeatID;
                int newSeatNrAlt;
                int newSeatIDAlt;



                if (seats.Count == 0)
                {


                    newSeatNr = seatChoice;
                    newSeatNr++;

                    newSeatNrAlt = seatChoice;
                    newSeatNrAlt--;

                    if (newSeatNrAlt > 0 && newSeatNrAlt <= rowLength)
                    {
                        newSeatIDAlt = wcf.GetSeatIDByNumberRow(newSeatNrAlt, rowChoice);
                    }
                    else
                    {
                        newSeatIDAlt = 0;
                    }

                    if (newSeatNr > 0 && newSeatNr <= rowLength)
                    {
                        newSeatID = wcf.GetSeatIDByNumberRow(newSeatNr, rowChoice);
                    }
                    else
                    {
                        newSeatID = 0;
                    }



                    if (wcf.CheckIfBooked(seatChoiceID, screeningID) == false)
                    {
                        seats.Add(seatChoiceID);
                    }

                    else
                    {

                        if (wcf.CheckIfBooked(newSeatID, screeningID) == false
                            && newSeatNr <= rowLength && newSeatID > 0)
                        {
                            seats.Add(newSeatID);

                        }

                        else if (wcf.CheckIfBooked(newSeatID, screeningID) == true && wcf.CheckIfBooked(newSeatIDAlt, screeningID) == false
                            && newSeatNrAlt <= rowLength && newSeatIDAlt > 0)
                        {
                            seats.Add(newSeatIDAlt);
                        }
                        else
                        {
                            seats.Add(0);
                        }

                    }


                }
                else if (seats.Count != 0)
                {

                    var firstSeat = (from s in seats
                                     select s).Min();
                    var lastSeat = (from s in seats
                                    select s).Max();

                    if (firstSeat > 0 && lastSeat > 0)
                    {
                        newSeatNr = wcf.GetSeatNumberByID(lastSeat);
                        newSeatNr++;

                        newSeatNrAlt = wcf.GetSeatNumberByID(firstSeat);
                        newSeatNrAlt--;
                    }
                    else
                    {
                        newSeatID = 0;
                        break;
                    }

                    if (newSeatNrAlt > 0 && newSeatNrAlt <= rowLength)
                    {
                        newSeatIDAlt = wcf.GetSeatIDByNumberRow(newSeatNrAlt, rowChoice);
                    }
                    else
                    {
                        newSeatIDAlt = 0;
                    }

                    if (newSeatNr > 0 && newSeatNr <= rowLength)
                    {
                        newSeatID = wcf.GetSeatIDByNumberRow(newSeatNr, rowChoice);
                    }
                    else
                    {
                        newSeatID = 0;
                    }

                    if (wcf.CheckIfBooked(newSeatID, screeningID) == false
                        && newSeatNr <= rowLength && newSeatID > 0)
                    {
                        seats.Add(newSeatID);
                    }

                    else if (wcf.CheckIfBooked(newSeatIDAlt, screeningID) == false && wcf.CheckIfBooked(newSeatIDAlt, screeningID) == false
                        && newSeatNrAlt <= rowLength && newSeatIDAlt > 0)
                    {
                        seats.Add(newSeatIDAlt);
                    }

                    else
                    {
                        seats.Add(0);
                    }
                }

            }
            var checkList = (from s in seats
                             select s);
            if (seats.Contains(0))
            {
                List<int> notPossible = new List<int>();
                notPossible.Add(0);

                return notPossible;
            }
            else
            {
                var list = (from s in seats
                            select s);
                return list.ToList();
            }
        }
    }
}

